package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.SysNotifyRecord;
import com.winiis.component.common.utils.db.DbUtils;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class SysNotifyRecordDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public SysNotifyRecordDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	/**
	 * to add new record to database,success return record id
	 *
	 * @param obj
	 * @return record id when success, otherwise return -1
	 */
	public int addSysNotifyRecord(SysNotifyRecord obj) {
		//if (!(obj.getAgentId() > 0)) {
			obj.setAgentId(CloudContextUtils.getAgentId());
		//}
		obj.setSendTime(new Date());
		String sql = "insert into sys_notify_record(tempName,msgType,sender,receiver," +
				"title,content,sendTime,state,agentId) values (?,?,?,?,?,?,?,?,?);";
		Object[] prm = {obj.getTempName(), obj.getMsgType(), obj.getSender(), obj.getReceiver(),
				obj.getTitle(), obj.getContent(), obj.getSendTime(), obj.getState(), 0};
		int ret = db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		} else {
			ret = DbUtils.lastId(this.db);
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delSysNotifyRecord(int id) {
		String sql = "delete from sys_notify_record where id=? and agentId=?";
		int ret = db.executeSql(sql, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query sys_notify_record item for detail
	public SysNotifyRecord querySysNotifyRecord(int id) {
		String sql = "select * from sys_notify_record where id=? and agentId=?";
		SysNotifyRecord ret = db.queryForBean(sql, SysNotifyRecord.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to query SysNotifyrecord record count
	public int querySysNotifyRecord(Date start, Date end) {
		String sql = "select * from sys_notify_record where sendtime>=? and sendtime<=? and agentId=?";
		Object[] params = {start, end, CloudContextUtils.getAgentId()};
		Long ret = (Long) db.queryFieldValue(sql, Long.class, params, 1);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		if (ret == null) {
			return -1;
		}
		return ret.intValue();
	}

	//to query SysNotifyrecord for record list with condition
	public List<SysNotifyRecord> querySysNotifyRecordList(Date start, Date end) {
		String sql = "select * from sys_notify_record where sendtime>=? and sendtime<=? and agentId=?";
		List<SysNotifyRecord> ret = db.queryForBeans(sql, SysNotifyRecord.class, new Object[]{start, end, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//query notify record list
	public List<SysNotifyRecord> querySysNotifyRecordList(Map<String, Object> cdt, int page, int pagesize) {
		List<Object> list = new ArrayList<>();
		StringBuilder sb = new StringBuilder("select * from sys_notify_record where 1=1  and agentId=?");

		list.add(CloudContextUtils.getAgentId());

		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			if (c.getKey().equals("start")) {
				sb.append(" and sendtime >=? ");
				list.add(c.getValue());
			} else if (c.getKey().equals("end")) {
				sb.append(" and sendtime <=? ");
				list.add(c.getValue());
			} else {
				sb.append(" and " + c.getKey() + "=? ");
				list.add(c.getValue());
			}
		}
		sb.append(" order by id desc ");
		String sql = sb.toString();
		if ((page > 0) && (pagesize > 0)) {
			sql = sql + String.format(" limit %d,%d", (page - 1) * pagesize, pagesize);
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}

		List<SysNotifyRecord> ret = db.queryForBeans(sql, SysNotifyRecord.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//query notify record Count
	public int querySysNotifyRecordCount(Map<String, Object> cdt) {
		List<Object> list = new ArrayList<>();
		StringBuilder sb = new StringBuilder("select count(id) from sys_notify_record where 1=1 and agentId=?");
		list.add(CloudContextUtils.getAgentId());
		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			if (c.getKey().equals("start")) {
				sb.append(" and sendtime >=?");
				list.add(c.getValue());
			} else if (c.getKey().equals("end")) {
				sb.append(" and sendtime <?");
				list.add(c.getValue());
			} else {
				sb.append(" and " + c.getKey() + "=?");
				list.add(c.getValue());
			}
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}

		Long ret = (Long) db.queryFieldValue(sb.toString(), Long.class, params, 1);
		this.setErrCode(db.getErrCode());
		return ret.intValue();
	}
}